package com.hongyuan.sellcoach.ui.main.view_mode;

import android.app.Application;
import android.os.Bundle;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.hongyuan.mvvmhabitx.binding.command.BindingAction;
import com.hongyuan.mvvmhabitx.binding.command.BindingCommand;
import com.hongyuan.mvvmhabitx.bus.event.SingleLiveEvent;
import com.hongyuan.mvvmhabitx.utils.ToastUtils;
import com.hongyuan.sellcoach.base.CustomViewModel;
import com.hongyuan.sellcoach.data.Constants;
import com.hongyuan.sellcoach.data.MyRepository;
import com.hongyuan.sellcoach.data.md5.BaseUtil;
import com.hongyuan.sellcoach.data.userbean.MemberLoginBean;
import com.hongyuan.sellcoach.ui.main.activity.LoginPassWordActivity;
import com.hongyuan.sellcoach.ui.main.activity.MainActivity;
import com.hongyuan.sellcoach.ui.main.activity.WebActivity;
import com.hongyuan.sellcoach.ui.main.beans.PhoneMessageTokenBean;
import com.hongyuan.sellcoach.utils.HourMeterUtil;
import cn.jpush.android.api.JPushInterface;

public class CodeLoginViewModel extends CustomViewModel<MyRepository> {

    private HourMeterUtil meterUtil;

    private int nowSecond = 60;

    //用户名的绑定
    public ObservableField<String> userName = new ObservableField<>("");
    //验证码的绑定
    public ObservableField<String> verificationCode = new ObservableField<>("");
    //验证码发生的文字描述
    public ObservableField<String> sendCodeText = new ObservableField<>("发送验证码");
    //是否同意协议
    public ObservableField<Boolean> isAgree = new ObservableField<>(false);

    //封装一个界面发生改变的观察者
    public UIChangeObservable uc = new UIChangeObservable();
    public class UIChangeObservable {
        //同意协议
        public SingleLiveEvent<Boolean> agree = new SingleLiveEvent<>();
        //显示阅读协议弹框
        public SingleLiveEvent<Boolean> yueduxieyi = new SingleLiveEvent<>();
    }

    public CodeLoginViewModel(@NonNull Application application, MyRepository model) {
        super(application, model);
    }

    //验证码发送点击
    public BindingCommand sendCode = new BindingCommand(new BindingAction() {
        @Override
        public void call() {

            if(nowSecond == 60){
                if(!BaseUtil.isValue(userName.get())){
                    ToastUtils.showShort("请输入手机号");
                    return;
                }
                setParams("m_mobile", userName.get());
                requestData(Constants.GET_MESSAGE_TOKEN);
            }

        }
    });

    //密码登录
    public BindingCommand goPasswordLogin = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            startActivity(LoginPassWordActivity.class);
        }
    });
    //登录
    public BindingCommand submit = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            if(isAgree.get()){
                codeLogin();
            }else{
                uc.yueduxieyi.call();
            }
        }
    });

    //服务协议
    public BindingCommand fwxy = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            Bundle bundle = new Bundle();
            bundle.putString("url","https://fit.1667799.com/html/xy.html");
            bundle.putString("title","用户协议");
            startActivity(WebActivity.class,bundle);
        }
    });
    //服务协议
    public BindingCommand yszc = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            Bundle bundle = new Bundle();
            bundle.putString("url","https://fit.1667799.com/html/ys.html");
            bundle.putString("title","隐私政策");
            startActivity(WebActivity.class,bundle);
        }
    });

    //同意协议
    public BindingCommand agreeXY = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            uc.agree.setValue(isAgree.get());
        }
    });

    @Override
    public void onCreate() {
        super.onCreate();

        meterUtil = new HourMeterUtil();
        meterUtil.setTimeCallBack(passedTime -> {
            if(passedTime > 0 && passedTime < 60){
                nowSecond = 60-passedTime;
                sendCodeText.set((60-passedTime)+"s");
            }else{
                if(passedTime >= 60){
                    nowSecond = 60;
                    meterUtil.stopCount();
                    sendCodeText.set("发送验证码");
                }
            }
        });
    }

    /*
    * 修改密码请求
    * */
    private void codeLogin(){
        if (TextUtils.isEmpty(userName.get())) {
            ToastUtils.showShort("请输入账号！");
            return;
        }

        if (TextUtils.isEmpty(verificationCode.get())) {
            ToastUtils.showShort("请输入验证码！");
            return;
        }

        clearParams().setParams("m_mobile", userName.get())
                .setParams("dxm",verificationCode.get());

        //短信登录
        requestData(Constants.MEMBERLOGIN_DXM);
    }

    @Override
    protected void returnData(int code, Object dataBean) {
        super.returnData(code, dataBean);

        if(code == Constants.GET_MESSAGE_TOKEN){
            PhoneMessageTokenBean tokenBean = (PhoneMessageTokenBean)dataBean;
            //开始倒计时
            meterUtil.startCount();

            //获取短信验证码
            setParams("m_mobile",userName.get()).setParams("sms_checked_code",tokenBean.getToken());
            requestData(Constants.SEND_REGI_MSG);
        }

        if(code == Constants.MEMBERLOGIN_DXM){
            MemberLoginBean bean = (MemberLoginBean) dataBean;

            if(bean.getRole_id() == 2){
                userToken.setM_id(bean.getM_id());
                userToken.setM_mobile(bean.getM_mobile());
                userToken.setRole_id(String.valueOf(bean.getRole_id()));

                //保存用户登录信息
                model.saveUser(bean);

                //极光推送设置别名或标签
                JPushInterface.setAlias(getApplication().getApplicationContext(), 100, userToken.getM_mobile());

                startActivity(MainActivity.class);
                finish();
            }else{
                ToastUtils.showShort("非教练用户");
            }

        }

    }
}
